package world.ztomorrow.gateway.auth;


import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import world.ztomorrow.common.constant.ErrorCodeEnum;
import world.ztomorrow.common.constant.SystemConstant;
import world.ztomorrow.common.exception.BusinessException;
import world.ztomorrow.gateway.utils.JWTUtils;

import java.util.Objects;

@Component
public class AuthStrategy {

    public Long authSSO(String token) {
        if (!StringUtils.hasText(token)) {
            // token 为空
            throw new BusinessException(ErrorCodeEnum.USER_LOGIN_EXPIRED);
        }
        Long userId = JWTUtils.verifyJWT(token, SystemConstant.NOVEL_FRONT_KEY);
        if (Objects.isNull(userId)) {
            // token 解析失败
            throw new BusinessException(ErrorCodeEnum.USER_LOGIN_EXPIRED);
        }

        // 返回 userId
        return userId;
    }
}
